package com.edu.tool.service.impl.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.tool.bean.user.SysLoginToken;
import com.edu.tool.mapper.user.SysLoginTokenMapper;
import com.edu.tool.service.user.SysLoginTokenService;
import com.edu.tool.util.date.SimpleDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service

public class SysLoginTokenServiceImpl  implements SysLoginTokenService {

    @Autowired
    private SysLoginTokenMapper loginTokenMapper;

    @Override
    public SysLoginToken createTokenPortal(Integer tenantId,
                                           String loginType,
                                           Integer userId,
                                           Integer contactId,
                                           Integer investAccountId,
                                           String clientIp,
                                           String loginChannel)
    {
        Date now = new Date();
        //默认6小时
        int hours = 6;
        if("wechat".equals(loginChannel) || "campaign_h5".equals(loginChannel)){
            //1年
            hours = 8760;
        }
        if("app".equals(loginChannel)){
            //1天
            hours = 24;
        }
        Date validTime = SimpleDateUtil.addHoursForDate(now, hours);
        SysLoginToken loginToken = new SysLoginToken();
        loginToken.setTenantId(tenantId);
        loginToken.setLoginType(loginType);
        loginToken.setUserId(userId);
        loginToken.setContactId(contactId);
        loginToken.setInvestAccountId(investAccountId);
        loginToken.setClientIp(clientIp);
        loginToken.setLoginChannel(loginChannel);
        loginToken.setCreate_time(now);
        loginToken.setUpdate_time(now);
        loginToken.setValidTime(validTime);
        loginToken.setToken(UUID.randomUUID().toString());
        loginToken.setPeriodType(hours);
        loginTokenMapper.insert(loginToken);
        return loginToken;
    }

    @Override
    public SysLoginToken getByid(String token){
        QueryWrapper<SysLoginToken> queryMapper = new QueryWrapper<SysLoginToken>();
        queryMapper.lambda().eq(SysLoginToken::getToken,token);
        SysLoginToken sysLoginToken = loginTokenMapper.selectOne(queryMapper);
        return sysLoginToken;
    }

    @Override
    public void refresh(Integer id, Integer hours){
        Date now = new Date();
        if(hours==null)
        {//默认6小时
            hours = 6;
        }
        Date validTime = SimpleDateUtil.addHoursForDate(now, hours);
        SysLoginToken record = new SysLoginToken();
        record.setId(id);
        record.setUpdate_time(now);
        record.setValidTime(validTime);
        loginTokenMapper.updateById(record);
    }

    @Override
    public Map check(String token){
        return new HashMap();
    }

    @Override
    public Map getByLoginChannel(Integer tenantId, String loginChannel){
        return new HashMap();
    }
}